Exploring The Data

Lets take a look at the data we got in our newly created .json file.

Number of Champions

numChampions <- nrow(champ_relations)
numChampions
## [1] 133

Column Names

colnames(champ_relations)
## [1] "friends"       "champion_name" "faction"       "rivals"

I dont like the order so lets change it up.

newchamp_relations <- champ_relations[,c(2,3,1,4)]

head(newchamp_relations)
##   champion_name     faction                  friends           rivals
## 1        Aatrox Independent                                Tryndamere
## 2   AurelionSol                                                      
## 3        Anivia    Freljord               Ashe, Nunu Brand, Lissandra
## 4         Akali       Ionia             Shen, Kennen              Zed
## 5          Ashe    Freljord Tryndamere, Anivia, Nunu          Sejuani
## 6         Amumu  BandleCity                    Annie
# lets try create a vector of all the champions
testVertexNames <- as.vector(newchamp_relations$champion_name)
head(testVertexNames)
## [[1]]
## [1] "Aatrox"
## 
## [[2]]
## [1] "AurelionSol"
## 
## [[3]]
## [1] "Anivia"
## 
## [[4]]
## [1] "Akali"
## 
## [[5]]
## [1] "Ashe"
## 
## [[6]]
## [1] "Amumu"
# this weird format is due to the JSON data
# lets just iterate through the champion names to store the vertexNames
vertexNames <- c()

for (champ in newchamp_relations$champion_name){
  vertexNames = c(vertexNames,tolower(champ))
}
# lets take a look at the names
head(vertexNames)
## [1] "aatrox"      "aurelionsol" "anivia"      "akali"       "ashe"       
## [6] "amumu"
#However, it turns out there is a relationship with a non champion!, Kled has a friend Skarl which appears in the data
vertexNames = c(vertexNames, "skaarl")

In order to add our edges of friends, we have to iterate through it again since JSON stores multiple friends in one pair with their respective champion, we cannot use the same iteration Edges are also more complex because we have to store the champion with its edge

Here is the data for friends

Creating the graph

Vertex creation / Nodes

head(newchamp_relations$friends)
## [[1]]
## [1] ""
## 
## [[2]]
## [1] ""
## 
## [[3]]
## [1] "Ashe" "Nunu"
## 
## [[4]]
## [1] "Shen"   "Kennen"
## 
## [[5]]
## [1] "Tryndamere" "Anivia"     "Nunu"      
## 
## [[6]]
## [1] "Annie"
# note the pair in [[2]]
# we have to create a better loop
# so we have to create an instance of the champion for each friend connection it has

#the original data 
edgeFriendsSource <- c() #empty edge for loop
edgeFriendsTarget <- c()

i = 1
f = 1

while(i <= numChampions) {
  #champName
  champName = newchamp_relations$champion_name[[i]]
  #how many friends?
  numFriends = length(newchamp_relations$friends[[i]])
  
  while (numFriends > 0 && f <= numFriends){
    if (newchamp_relations$friends[[i]][f] !="")
      {
      edgeFriendsSource = c(edgeFriendsSource,tolower(champName))
      edgeFriendsTarget = c(edgeFriendsTarget, tolower(newchamp_relations$friends[[i]][f]))
    }
    f = f + 1
    }
  f = 1
  i = i +1
}
head(edgeFriendsSource)
## [1] "anivia" "anivia" "akali"  "akali"  "ashe"   "ashe"
head(edgeFriendsTarget)
## [1] "ashe"       "nunu"       "shen"       "kennen"     "tryndamere"
## [6] "anivia"

Create Vertex Attributes

Lets create the attributes for the faction of champions

i = 1
f = 1
edgeFactionSource <- c() #empty edge for loop
edgeFactionTarget <- c()
while(i <= numChampions) {
  #champName
  numFactions = length(newchamp_relations$faction[i])
  for (faction in newchamp_relations$faction){
    if (f < 134) {
    if (newchamp_relations$faction[[f]] !="" && f < 134)
    {
      champName = newchamp_relations$champion_name[[f]] #133
      edgeFactionSource = c(edgeFactionSource, tolower(champName))
      edgeFactionTarget = c(edgeFactionTarget, newchamp_relations$faction[[f]])
    }
    f = f + 1
    }
  }
  i = i+1
}
head(edgeFactionSource)
## [1] "aatrox" "anivia" "akali"  "ashe"   "amumu"  "azir"
head(edgeFactionTarget)
## [1] "Independent" "Freljord"    "Ionia"       "Freljord"    "BandleCity" 
## [6] "Shurima"

Creating Connections/Edges

Lets create the rival connections for the graph

edgeRivals <- c() #empty edge for loop

i = 1
f = 1

while(i <= numChampions) {
  #champName
  champName = newchamp_relations$champion_name[[i]]
  #how many rivals?
  numRivals = length(newchamp_relations$rivals[[i]])
  
  while (numRivals > 0 && f <= numRivals){
    if (newchamp_relations$rivals[[i]][f] !="")
      {
      edgeRivals = c(edgeRivals,tolower(champName), tolower(newchamp_relations$rivals[[i]][f]))
    }
    f = f + 1
    }
  f = 1
  i = i +1
}
edgeRivals[276] = "wukong" # it was labeled as monkeyking, which threw errors in the graph

Graphing the data

# globals
vertexSize <- 7
vLabelSize <- 1
vLabelDist <- 0
vLabelColor <- "darkblue"
vLabelFont <- 2 # bold text
vLabelDegree = -pi/2
eArrowSize <- 1
eArrowWidth<- .7
l <- layout.fruchterman.reingold

Graph of both

dfEdge = as.data.frame(edgeFriendsSource, stringsAsFactors=FALSE)
dfEdge["friendsTarget"] = edgeFriendsTarget

# vertex dataframe
dfVertex = as.data.frame(vertexNames, stringsAsFactors=FALSE)
dfVertex["ID"] = vertexNames

both = graph_from_data_frame(d=dfEdge, vertices = dfVertex, directed = T)
E(both)$color <- "darkgreen" # green for friends
both <- add_edges(both, edgeRivals, attr=list(color="red")) #red for enemies

both_layout <- layout.fruchterman.reingold(both, niter=10000)

plot(both, layout = both_layout)

#awesome, it works

Here we add factions to the graph

# add factions

V(both)$Faction = as.character(edgeFactionTarget[match(V(both)$name, edgeFactionSource)])
head(V(both)$Faction)
## [1] "Independent" NA            "Freljord"    "Ionia"       "Freljord"   
## [6] "BandleCity"
allFactions = unique(V(both)$Faction)
factionColors <- c('red',NA,'lightblue','blue','brown','gold','lawngreen','orange',NA,'purple', 'black','darkgreen','darkgray','ivory','darkblue',NA,NA,NA,NA,NA)

# add a key for colors
factionNumber <- function ( faction ) {
  match( faction, allFactions )}

V(both)$color <- factionColors[factionNumber(V(both)$Faction)]

graph both

plot(both, layout = both_layout)

This is very ugly so let’s try and fix it up.

V(both)$size <- vertexSize
V(both)$label.cex <- vLabelSize
V(both)$label.dist <- vLabelDist
V(both)$label.color <- vLabelColor
V(both)$label.font <- vLabelFont
V(both)$label.degree = vLabelDegree
E(both)$arrow.size <- eArrowSize
E(both)$arrow.width <- eArrowWidth

plot(both, layout=both_layout, asp = 0, frame = TRUE, main = "Champion Relationships")

Friends graph

friends <- graph_from_data_frame(d=dfEdge, vertices = dfVertex, direct = T)

E(friends)$color <- "darkgreen" # green for friends
V(friends)$Faction = as.character(edgeFactionTarget[match(V(friends)$name, edgeFactionSource)])

#Factions added!

allFactions = unique(V(friends)$Faction)
factionColors <- c('red',NA,'lightblue','blue','brown','gold','lawngreen','orange',NA,'purple', 'black','darkgreen','darkgray','ivory','darkblue',NA,NA,NA,NA,NA)

V(friends)$color <- factionColors[factionNumber(V(friends)$Faction)]
V(friends)$size <- vertexSize
V(friends)$label.cex <- vLabelSize
V(friends)$label.dist <- vLabelDist
V(friends)$label.color <- vLabelColor
V(friends)$label.font <- vLabelFont
V(friends)$label.degree = vLabelDegree
E(friends)$arrow.size <- eArrowSize
E(friends)$arrow.width <- eArrowWidth

#pdf("friends.pdf",10,10) #saves graph as a pdf

plot(friends, frame = TRUE, main = "Champion Friendships")

# legend(x=-1.5, y=-1.1, c("Newspaper","Television", "Online News"), pch=21,
# 
#        col="#777777", pt.bg=colrs, pt.cex=2, cex=.8, bty="n", ncol=1)

Rivals graph

rivals <- make_empty_graph() + vertices ( vertexNames)

rivals <- add_edges(rivals, edgeRivals, attr=list(color="red")) #red for enemies
E(rivals)$color <- "darkred" # red for rivals
V(rivals)$Faction = as.character(edgeFactionTarget[match(V(rivals)$name, edgeFactionSource)])

#Factions added!

allFactions = unique(V(rivals)$Faction)
factionColors <- c('red',NA,'lightblue','blue','brown','gold','lawngreen','orange',NA,'purple', 'black','darkgreen','darkgray','ivory','darkblue',NA,NA,NA,NA,NA)

V(rivals)$color <- factionColors[factionNumber(V(rivals)$Faction)]
V(rivals)$size <- vertexSize
V(rivals)$label.cex <- vLabelSize
V(rivals)$label.dist <- vLabelDist
V(rivals)$label.color <- vLabelColor
V(rivals)$label.font <- vLabelFont
V(rivals)$label.degree = vLabelDegree
E(rivals)$arrow.size <- eArrowSize
E(rivals)$arrow.width <- eArrowWidth

plot(rivals, layout=l, asp = 0, frame = TRUE, main = "Champion Rivals")

Graph Calculations

Centrality

centr_degree(rivals)$centralization
## [1] 0.490757
centr_clo(rivals, mode = "all")$centralization
## [1] 0.3333943
head(centr_betw(rivals)$res)
## [1] 0 0 0 0 0 0
centr_eigen(rivals)$centralization
## [1] 0.9131442

Betweenness

highest <- max(betweenness(rivals))
index_of_highest <- match(highest, betweenness(rivals))
print(index_of_highest)
## [1] 18
rivals[[index_of_highest]]
## $darius
## + 3/134 vertices, named:
## [1] draven   katarina vladimir
#darius is the point of highest connection between all rivals

highest <- max(betweenness(friends))
index_of_highest <- match(highest, betweenness(friends))
print(index_of_highest) 
## [1] 32
friends[[index_of_highest]] 
## $garen
## + 3/134 vertices, named:
## [1] jarvaniv lux      xinzhao
#garen between friends

highest <- max(betweenness(both)) 
index_of_highest <- match(highest, betweenness(both)) 
print(index_of_highest) 
## [1] 32
both[[index_of_highest]] 
## $garen
## + 6/134 vertices, named:
## [1] jarvaniv katarina lux      swain    urgot    xinzhao
#garen again

Closeness Least Friends, Rivals, Both

closest <- min(closeness(rivals))
index_of_closest <- match(closest, closeness(rivals))
print(index_of_closest)
## [1] 2
rivals[[index_of_closest]]
## $aurelionsol
## + 0/134 vertices, named:
#aurelionsol rivals

closest <- min(closeness(friends))
index_of_closest <- match(closest, closeness(friends))
print(index_of_closest) 
## [1] 1
friends[[index_of_closest]] 
## $aatrox
## + 0/134 vertices, named:
#aatrox friends

closest <- min(closeness(both)) 
index_of_closest <- match(closest, closeness(both)) 
print(index_of_closest) 
## [1] 2
both[[index_of_closest]] 
## $aurelionsol
## + 0/134 vertices, named:
#aurelionsol again

Closeness Most Friends, Rivals, Both

most <- max(closeness(rivals))
index_of_most <- match(most, closeness(rivals))
print(index_of_most)
## [1] 55
rivals[[index_of_most]]
## $kled
## + 133/134 vertices, named:
##   [1] aatrox       aurelionsol  anivia       akali        ashe        
##   [6] amumu        azir         annie        bard         blitzcrank  
##  [11] brand        caitlyn      braum        ahri         cassiopeia  
##  [16] chogath      alistar      darius       diana        corki       
##  [21] draven       drmundo      evelynn      ekko         ezreal      
##  [26] fiddlesticks fiora        elise        fizz         galio       
##  [31] gangplank    garen        gnar         gragas       graves      
##  [36] hecarim      illaoi       heimerdinger irelia       ivern       
##  [41] janna        jarvaniv     jax          jayce        jhin        
##  [46] jinx         kalista      karma        kassadin     katarina    
## + ... omitted several vertices
#kled most rivals

most <- max(closeness(friends))
index_of_most <- match(most, closeness(friends))
print(index_of_most) 
## [1] 40
friends[[index_of_most]] 
## $ivern
## + 3/134 vertices, named:
## [1] bard   lulu   maokai
#ivern friends (3)

most <- max(closeness(both)) 
index_of_most <- match(most, closeness(both)) 
print(index_of_most) 
## [1] 55
both[[index_of_most]] 
## $kled
## + 134/134 vertices, named:
##   [1] aatrox       aurelionsol  anivia       akali        ashe        
##   [6] amumu        azir         annie        bard         blitzcrank  
##  [11] brand        caitlyn      braum        ahri         cassiopeia  
##  [16] chogath      alistar      darius       diana        corki       
##  [21] draven       drmundo      evelynn      ekko         ezreal      
##  [26] fiddlesticks fiora        elise        fizz         galio       
##  [31] gangplank    garen        gnar         gragas       graves      
##  [36] hecarim      illaoi       heimerdinger irelia       ivern       
##  [41] janna        jarvaniv     jax          jayce        jhin        
##  [46] jinx         kalista      karma        kassadin     katarina    
## + ... omitted several vertices
#kled again

Eigenvector Centrality, most influential vertex

influential <- max(evcent(rivals)$vector)
index_of_influential <- match(influential, evcent(rivals)$vector)
print(index_of_influential)
## [1] 55
rivals[[index_of_influential]]
## $kled
## + 133/134 vertices, named:
##   [1] aatrox       aurelionsol  anivia       akali        ashe        
##   [6] amumu        azir         annie        bard         blitzcrank  
##  [11] brand        caitlyn      braum        ahri         cassiopeia  
##  [16] chogath      alistar      darius       diana        corki       
##  [21] draven       drmundo      evelynn      ekko         ezreal      
##  [26] fiddlesticks fiora        elise        fizz         galio       
##  [31] gangplank    garen        gnar         gragas       graves      
##  [36] hecarim      illaoi       heimerdinger irelia       ivern       
##  [41] janna        jarvaniv     jax          jayce        jhin        
##  [46] jinx         kalista      karma        kassadin     katarina    
## + ... omitted several vertices
#kled most rivals

influential <- max(evcent(friends)$vector)
index_of_influential <- match(influential, evcent(friends)$vector)
print(index_of_influential)
## [1] 12
friends[[index_of_influential]]
## $caitlyn
## + 3/134 vertices, named:
## [1] ezreal jayce  vi
#ivern friends (3)

most <- max(evcent(both)$vector)
index_of_influential <- match(influential, evcent(both)$vector)
print(index_of_influential)
## [1] 55
both[[index_of_influential]]
## $kled
## + 134/134 vertices, named:
##   [1] aatrox       aurelionsol  anivia       akali        ashe        
##   [6] amumu        azir         annie        bard         blitzcrank  
##  [11] brand        caitlyn      braum        ahri         cassiopeia  
##  [16] chogath      alistar      darius       diana        corki       
##  [21] draven       drmundo      evelynn      ekko         ezreal      
##  [26] fiddlesticks fiora        elise        fizz         galio       
##  [31] gangplank    garen        gnar         gragas       graves      
##  [36] hecarim      illaoi       heimerdinger irelia       ivern       
##  [41] janna        jarvaniv     jax          jayce        jhin        
##  [46] jinx         kalista      karma        kassadin     katarina    
## + ... omitted several vertices
#kled again

Images in Graph

imgname <- list()
imgfilename <- list()
  
for (x in 1:134)
    {
    imgname = c(imgname, paste(vertexNames[x], ".png", sep =""))
    imgfilename <- c(imgfilename, file.path(path_to_files,imgname[[x]]))
    }
set.seed(1)
# arrows point to x is friends with

l <- layout.norm(layout.fruchterman.reingold(friends, niter = 500, coolexp = .6))
## Warning in layout_with_fr(structure(list(134, TRUE, c(2, 2, 3, 3, 4, 4, :
## Argument `coolexp' is deprecated and has no effect
V(friends)$label.cex <- .01
V(friends)$size <- .008
E(friends)$arrow.size <- 1.25
#E(friends)$arrow.width <- 1

plot(friends, layout = l, frame = TRUE, main = "Champion Relationships")

img <- lapply(imgfilename, png::readPNG)
## Warning in FUN(X[[i]], ...): libpng warning: iCCP: known incorrect sRGB
## profile
for(i in 1:nrow(l)) {  
  rasterImage(img[[i]], l[i, 1]-0.02, l[i, 2]-0.02, l[i, 1]+0.02, l[i, 2]+0.02)
}

set.seed(1)
# arrows point to x is friends with
l <- layout.norm(layout.fruchterman.reingold(rivals, niter = 500, coolexp = .6))
## Warning in layout_with_fr(structure(list(134, TRUE, c(0, 2, 2, 3, 4, 6, :
## Argument `coolexp' is deprecated and has no effect
V(rivals)$label.cex <- .01
V(rivals)$size <- .008
E(rivals)$arrow.size <- 1.25
E(rivals)$arrow.width <- .5

plot(rivals, layout = l, frame = TRUE, main = "Champion Relationships")

img <- lapply(imgfilename, png::readPNG)
## Warning in FUN(X[[i]], ...): libpng warning: iCCP: known incorrect sRGB
## profile
for(i in 1:nrow(l)) {  
  rasterImage(img[[i]], l[i, 1]-0.02, l[i, 2]-0.02, l[i, 1]+0.02, l[i, 2]+0.02)
}

set.seed(1)
# arrows point to x is friends with

l <- layout.norm(layout.fruchterman.reingold(both, niter = 500, coolexp = .6))
## Warning in layout_with_fr(structure(list(134, TRUE, c(2, 2, 3, 3, 4, 4, :
## Argument `coolexp' is deprecated and has no effect
V(both)$label.cex <- .01
V(both)$size <- .008
E(both)$arrow.size <- 1.25
E(both)$arrow.width <- 1

plot(both, layout = l, frame = TRUE, main = "Champion Relationships")

img <- lapply(imgfilename, png::readPNG)
## Warning in FUN(X[[i]], ...): libpng warning: iCCP: known incorrect sRGB
## profile
for(i in 1:nrow(l)) {  
  rasterImage(img[[i]], l[i, 1]-0.02, l[i, 2]-0.02, l[i, 1]+0.02, l[i, 2]+0.02)
}